﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using BMSQ.Utility;

namespace BMSQ.Admin
{
    /// <summary>
    /// 页面层(表示层)基类,所有页面继承该页面
    /// </summary>
    public class PageBase : System.Web.UI.Page
    {
        string virtualPath = ConfigHelper.GetConfigString("VirtualPath");

        /// <summary>
        /// 构造函数
        /// </summary>
        public PageBase()
        {
            //this.Load+=new EventHandler(PageBase_Load);
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new System.EventHandler(PageBase_Load);
            this.Error += new System.EventHandler(PageBase_Error);
        }
        //错误处理
        protected void PageBase_Error(object sender, System.EventArgs e)
        {
            string errMsg;
            Exception currentError = Server.GetLastError();
            errMsg = "<link rel=\"stylesheet\" href=\"/style.css\">";
            errMsg += "<h1>系统错误：</h1><hr/>系统发生错误， " +
                "该信息已被系统记录，请稍后重试或与管理员联系。<br/>" +
                "错误地址： " + Request.Url.ToString() + "<br/>" +
                "错误信息： <font class=\"ErrorMessage\">" + currentError.Message.ToString() + "</font><hr/>" +
                "<b>Stack Trace:</b><br/>" + currentError.ToString();
            Response.Write(errMsg);
            Server.ClearError();

        }
        private void PageBase_Load(object sender, EventArgs e)
        {
            if (Session["UserInfo"] == null)
            {
                Session.Clear();
                Session.Abandon();
                Response.Redirect(Common.VirtualPath() + "/Login.aspx");
            }

            //if (Common.UserInfo == null)
            //{
            //    FormsAuthentication.SignOut();
            //    Session.Clear();
            //    Session.Abandon();
            //    Response.Clear();
            //    Response.Write("<script defer>window.alert('当前登录用户已过期！\\n请重新登录！');parent.location='" + virtualPath + "/Login.aspx';</script>");
            //    Response.End();
            //}

            //if (!Page.IsPostBack)
            //{
            //    //权限验证
            //    if (Context.User.Identity.IsAuthenticated)
            //    {
            //        if (Session["UserInfo"] == null)
            //        {
            //            Session.Clear();
            //            Session.Abandon();
            //            Response.Redirect("Login.aspx");
            //        }
            //    }
            //    else
            //    {
            //        FormsAuthentication.SignOut();
            //        Session.Clear();
            //        Session.Abandon();
            //        Response.Clear();
            //        Response.Write("<script defer>window.alert('您没有权限进入本页或当前登录用户已过期！\\n请重新登录或与管理员联系！');parent.location='" + virtualPath + "/Login.aspx';</script>");
            //        Response.End();
            //    }
            //}

        }

        public void BindDrowDownList(DropDownList ddl, object source, string displayField, string valueField, bool firstEmpty)
        {
            ddl.DataSource = source;
            ddl.DataTextField = displayField;
            ddl.DataValueField = valueField;
            ddl.DataBind();
            if (firstEmpty)
            {
                ddl.Items.Insert(0, new ListItem("", ""));
            }
        }
    }
}